import axios from 'axios'
import { getToken } from './auth.js'

import appConfig from '../config.js'
axios.defaults.baseURL = appConfig.apiUrl

axios.defaults.timeout = 15000
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
//axios.defaults.headers.post['Authorization'] = 'Bearer ' + token;


// axios.defaults.headers.post['Content-Type'] = 'multipart/form-data'
// 请求拦截
axios.interceptors.request.use(

	config => {
		console.log(config.headers)
		config.headers['Authorization'] = 'Bearer ' + getToken()
		// const { configId } = store.state.config
		// const { PUserId: userId } = store.state.user.userInfo
		// 设置项目配置id和用户id
		// config.data.wxConfigId = configId 
		// config.data.userId = userId || 0
	
		return config
	},
	err => {
		return Promise.reject(err)
	}
)

// 响应拦截
axios.interceptors.response.use(
	response => {
		if (response.status === 200) {
			return Promise.resolve(response.data)
		}
	}, 
	error => {
		return Promise.reject(error)
	}
)

//自己定义个适配器，用来适配uniapp的语法
axios.defaults.adapter = function(config) { 	
    return new Promise((resolve, reject) => {        
        var settle = require('axios/lib/core/settle');
        var buildURL = require('axios/lib/helpers/buildURL');
		console.log('adapter')
        uni.request({
            method: config.method.toUpperCase(),
            url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {                
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                };

                settle(resolve, reject, response);
            }
        })
    })
}

const get = (url, params) => {
  if(params && typeof params == 'object'){
    for(let i in params){
      if(params[i]==="" || params[i] == null)delete params[i];
    }
  }
  // _showLoading = showLoading;
  // axios.defaults.headers[_Authorization] = getToken();
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params    
    })
      .then(res => {
        // if (response.status == 401) {
				  // getNewToken(() => { get(url, param, _showLoading); });
        //  return;
        // }

        resolve(res)
      }, err => {
        // if (err.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        // redirect(response.data);
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

const post = (url, params,cfg) => {
  if(params && typeof params == 'object'){
    for(let i in params){
      if(params[i]==="" || params[i] == null)delete params[i];
    }
  }
  return new Promise((resolve, reject) => {
    axios.post(url, params,cfg)
      .then(res => {        
        // if (response.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        resolve(res)
      }, err => {
        // if (err.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        // redirect(response.data);
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

const put = (url, params) => {
  if(params && typeof params == 'object'){
    for(let i in params){
      if(params[i]==="" || params[i] == null)delete params[i];
    }
  }
  return new Promise((resolve, reject) => {
    axios.put(url, params)
      .then(res => {
        // if (response.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        resolve(res)
      }, err => {
        // if (err.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        // redirect(response.data);
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
const del = (url, params) => {
  if(params && typeof params == 'object'){
    for(let i in params){
      if(params[i]==="" || params[i] == null)delete params[i];
    }
  } 
  return new Promise((resolve, reject) => {
    axios.delete(url, {
      data: params
    })
      .then(res => {
        // if (response.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        resolve(res)
      }, err => {
        // if (err.status == 202) {
        //   getNewToken(() => { get(url, param, _showLoading); });
        //   return;
        // }
        // redirect(response.data);
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export default {
  get,
  post,
  put,
  del
}

